package com.icode.dao.impl;

import com.icode.core.model.about.*;
import com.icode.dao.HomeAboutDao;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-3-5
 * Time: 上午10:20
 * To change this template use File | Settings | File Templates.
 */
@Repository("homeAboutDao")
public class HomeAboutDaoImpl extends EntityDaoImpl implements HomeAboutDao {


    @Override
    public void saveOrUpdateObject(Object object) {
        currentSession().save(object);
    }

    @Override
    public Introduce findIntroduce() {
        String hql = "from " + Introduce.class.getName();
        Query query = currentSession().createQuery(hql);
        List<Introduce> introduces = query.list();
        return introduces.isEmpty() ? null : introduces.get(0);
    }

    @Override
    public Introduce findIntroduceById(String introduceId) {
        return (Introduce) currentSession().get(Introduce.class, Integer.parseInt(introduceId));
    }

    @Override
    public Model findModel() {
        String hql = "from " + Model.class.getName();
        Query query = currentSession().createQuery(hql);
        List<Model> models = query.list();
        return models.isEmpty() ? null : models.get(0);
    }

    @Override
    public Model findModelById(String modelId) {
        return (Model) currentSession().get(Model.class, Integer.parseInt(modelId));
    }

    @Override
    public Concept findConceptById(String conceptId) {
        return (Concept) currentSession().get(Concept.class, Integer.parseInt(conceptId));
    }

    @Override
    public Concept findConcept() {
        String hql = "from " + Concept.class.getName();
        Query query = currentSession().createQuery(hql);
        List<Concept> concepts = query.list();
        return concepts.isEmpty() ? null : concepts.get(0);
    }

    @Override
    public Cooperation findCooperationById(String cooperationId) {
        return (Cooperation) currentSession().get(Cooperation.class, Integer.parseInt(cooperationId));
    }

    @Override
    public Cooperation findCooperation() {
        String hql = "from " + Cooperation.class.getName();
        Query query = currentSession().createQuery(hql);
        List<Cooperation> cooperations = query.list();
        return cooperations.isEmpty() ? null : cooperations.get(0);
    }

    @Override
    public SubHead findSubHeadById(String subHeadId) {
        return (SubHead) currentSession().get(SubHead.class, Integer.parseInt(subHeadId));
    }

    @Override
    public void deleteSubByObjectId(Object object) {
        currentSession().delete(object);
    }

    @Override
    public List<News> findNews() {
        String hql = "from " + News.class.getName() + " a order by a.id";
        Query query = currentSession().createQuery(hql);
        return query.list();
    }

    @Override
    public void deleteObject(Object object) {
        currentSession().delete(object);
    }
}
